import time
from telnetlib import EC
from time import sleep

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from base.public import GetIniDate
from  base.public import  GetYmalData
Timeout=8 #等待时长
class Basedriver:
    def __init__(self,url):
        '''
        打开网站
        :param url:
        '''
        driver=webdriver.Chrome()
        try:
            self._base_driver=driver
        except NameError:
            raise NameError("错误")
        self._base_driver.get(url)
    def implictly_wait(self,second):
        '''
        隐式等待
        :param second:等待时间
        :return:
        '''
        self._base_driver.implicitly_wait(second)

    def _web_driver_wait(self, selector, timeout=Timeout, poll_frequency=0.5):
        """
        显式等待
        :return:
        """
        locator = self.__convert_data__(selector)
        eme_selector = WebDriverWait(self._base_driver, timeout, poll_frequency).until(
            EC.presence_of_element_located(locator))
        return eme_selector
    def max_window(self):
        '''
        窗口最大化
        :return:
        '''
        self._base_driver.maximize_window()

    def click(self,selector):
        '''
        单击击元素
        :param selector:
        :return:
        '''
        locator = self.__convert_data__(selector)
        self._base_driver.find_element(*locator).click()
    def double_click(self,selector):
        '''
        双击鼠标
        :param selector:
        :return:
        '''
        locator=self.__convert_data__(selector)
        ActionChains(self._base_driver.find_element(*locator)).double_click()
    def mouse_element(self,selector):
        '''
        鼠标停留在摸个元素
        :return:
        '''
        locator=self.__convert_data__(selector)
        ActionChains(self._base_driver).move_to_element(self._base_driver.find_element(*locator)).perform()



    def send_key(self,selector,text):
        '''
        输入文本
        :param selector:
        :param text: 要输入的文本
        :return:
        '''
        # self._web_driver_wait(selector).clear()
        # locator = self.__convert_data__(selector)
        # print(locator)
        # self._base_driver.find_element(*locator).send_keys(text)
        self._web_driver_wait(selector).send_keys(text)

    def get_text(self,select):
        '''
        获取元素的文本
        :return:
        '''
        ele_text=self.__convert_data__(select)
        return self._base_driver.find_element(ele_text).text

    def switch_to_frame(self, selector):
        """
        进入iframe
        :param selector:
        :return:
        """
        iframe = self.__convert_data__(selector)
        ele=self._base_driver.find_element(iframe)
        self._base_driver.switch_to.frame(ele)
    def close(self):
        '''
        关闭窗口
        :return:
        '''
        self._base_driver.close()
    def sleeptime(self,sleeptime):
       '''
       睡眠
       :param sleeptime:睡眠时间
       :return:
       '''

       sleeptime(sleeptime)
    def refresh_windows(self):
        '''
        刷新页面
        :return:
        '''
        self._base_driver.refresh()
    def quit_windows(self):
        '''
        关闭浏览器进程
        :return:
        '''
        self._base_driver.quit()
    def __convert_data__(self,selector):
            '''
            将selector数据(id,account)转换为(By.ID,account)格式
            :param selector:
            :return:
            '''
            try:
                selector_by=selector.split(",")[0].strip()
                selector_value=selector.split(",")[1].strip()
            except IndexError:
                selector_by=selector.split("=")[0].strip()
                selector_value=selector.split("=")[1].strip()
            if selector_by=="id":
                locator=(By.ID,selector_value)
            elif selector_by=="xpath":
                locator=(By.XPATH,selector_value)
            elif selector_by=="name":
                locator=(By.NAME,selector_value)
            elif selector_by=="class":
                locator=(By.CLASS_NAME,selector_value)
            elif selector_by=="name":
                locator=(By.NAME,selector_value)
            else:
                raise NameError("输入的参数不对,或者是我没加")
            return  locator

def get_driver():
    '''
    返回一个dirver对象。
    :return:
    '''
    driver=Basedriver(GetIniDate().get_init_data("URL","ProjectUrl"))
    return driver

class Boxdriver():
    def __init__(self,driver):
        self.basedriver=driver

if __name__=='__main__':
    # get_driver()
    res=GetYmalData().get_ymal_data('F:\有妖气TEST\elements\login_page.yaml')["百度一下"]
    a = GetYmalData().get_ymal_data('F:\有妖气TEST\elements\login_page.yaml')["登录"]
    b = GetYmalData().get_ymal_data('F:\有妖气TEST\elements\login_page.yaml')["用户名"]
    c = GetYmalData().get_ymal_data('F:\有妖气TEST\elements\login_page.yaml')["切换"]
    d=GetYmalData().get_ymal_data('F:\有妖气TEST\elements\login_page.yaml')["输入框"]
    print(d)
    w=Basedriver(GetIniDate().get_init_data("URL","ProjectUrl"))
    w.send_key(d,"张嘉文")





